package com.cj.util;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Component;

import cn.hutool.core.convert.Convert;

@Component
public class RedisObject {
	
	//事务执行，临时写下。后面再完善
	public void multiExec(RedisTemplate<String, String> redisTemplate,List<String> keyTimeList){
		String newsKey_prefix="hnews";//新闻key的前缀
		String newsClickSet="newsclick" ;//保存新闻 点击量集合的集合名称
		String newsCache_key="newscache";//保存新闻时间戳的key
		
		//管道执行
		List<Object> txResults =  redisTemplate.execute(new SessionCallback<List<Object>>() {
			@Override
			public List<Object> execute(RedisOperations operations) throws DataAccessException {
				operations.multi();
				for (String timeKey : keyTimeList) {
					Double score = operations.opsForZSet().score(newsCache_key, timeKey);
					Long newsId = Convert.convert(Long.class,score);
					String newsClickKey="news_id"+newsId;//保存新闻点击量 的成员名
					String newsKey=newsKey_prefix+newsId;  //拼凑成 一个完整的新闻key
					operations.opsForHash().delete(newsKey, "news_id"); //删除新闻数据的Hash
					operations.opsForHash().delete(newsKey, "news_title");
					operations.opsForZSet().remove(newsClickSet, newsClickKey); //删除点击量的Zset
					operations.opsForZSet().remove(newsCache_key,timeKey);//删除newscache 里面的对应值
				}
				return operations.exec();
			}
		});
		//io.lettuce.core.RedisCommandExecutionException: ERR EXEC without MULTI
		/*redisTemplate.multi();
		for (String timeKey : keyTimeList) {
			Double score = redisTemplate.opsForZSet().score(newsCache_key, timeKey);
			Long newsId = Convert.convert(Long.class,score);
			String newsClickKey="news_id"+newsId;//保存新闻点击量 的成员名
			String newsKey=newsKey_prefix+newsId;  //拼凑成 一个完整的新闻key
			redisTemplate.opsForHash().delete(newsKey, "news_id"); //删除新闻数据的Hash
			redisTemplate.opsForHash().delete(newsKey, "news_title");
			redisTemplate.opsForZSet().remove(newsClickSet, newsClickKey); //删除点击量的Zset
			redisTemplate.opsForZSet().remove(newsCache_key,timeKey);//删除newscache 里面的对应值
		}
		redisTemplate.exec();*/
	}
	
	public void multiExec(RedisTemplate<String, String> redisTemplate,RedisConnection redisConnection,int db,Long newsId){
		//DOTO 选择数据库 ，重要
		List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
			@Override
			public List<Object> execute(RedisOperations operations) throws DataAccessException {
				operations.multi();
				operations.opsForHash().delete("hnews"+newsId, "news_id"); //删除新闻数据的Hash
				operations.opsForHash().delete("hnews"+newsId, "news_title");
				operations.opsForZSet().remove("newsclick", "news_id"+newsId); //删除点击量的Zset
				return operations.exec();
			}
		});
	}
	
	  /**
	      * 统计bit位为1的总数
     * @param key
     */
    public long bitCount(final String key, RedisTemplate<String, String> redisTemplate) {
    	return redisTemplate.execute(new RedisCallback<Long>() {
    		@Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
    			long result = 0;
                result = connection.bitCount(key.getBytes());
                return result;
            }
        });
    }
    
    public byte[] getBits(final String key, RedisTemplate<String, String> redisTemplate) {
    	return redisTemplate.execute(new RedisCallback<byte[]>() {
    		@Override
            public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
    			long result = 0;
                byte[] range = connection.getRange(key.getBytes(), 0, -1);
                return range;
            }
        });
    }

	


}
